//object Memory.

#include "out_Memoire.h"
#include "out_compilateur.h" //for the mnemonics

using namespace std;

Memoire::Memoire(){
	for(int i=0; i<TAILLE_MEM; i++){
		m_tmemoire[i]=Instruction();
	}
}

void Memoire::raz(){	//clear the whole memory
	for(int i=0; i<TAILLE_MEM; i++){
		m_tmemoire[i].raz();
	}
}

void Memoire::raz(int a, int b){ //clear the memory between a and b
	for(int i=a; i<b; i++){
		m_tmemoire[i].raz();
	}
}

void Memoire::setCouleur(int aa, int bb, SDL_Surface *ecran, Uint32 couleurs[], int mode){	//set the color of the memory between a and b.
	int a=aa%TAILLE_MEM;
	int b=bb%TAILLE_MEM;
	Uint32 coul=0;
	if(mode==0){	//if mode == 1, we draw the cell in red (this is the position of the "instruction pointer"
		for(int i=a; i<=b; i++){
			int x=((i%90)*PAS), y=((i/90)*PAS);
			switch(m_tmemoire[i].getOp()){	//the color of each cell is function of the operation.
				case DAT:
					if(m_tmemoire[i].getArgA()==0)	//it is a DAT 0
						coul=couleurs[C_BLEU_FONCE];
					else	//it is DAT X
						coul=couleurs[C_BLEU];
					break;
				case JMP:
					coul=couleurs[C_VERT];
					break;
				case MOV:
					coul=couleurs[C_JAUNE];
					break;
				case ADD:
					coul=couleurs[C_MAGENTA];
					break;
				case SUB:
					coul=couleurs[C_OCRE];
					break;
				case JMZ:
					coul=couleurs[C_CYAN];
					break;
				case JMG:
					coul=couleurs[C_GRIS];
					break;
				case DJZ:
					coul=couleurs[C_NOIR];
					break;
				case CMP:
					coul=couleurs[C_GRIS_CLAIR];
					break;
			}
			rect(x+1, y+1, PAS-1, PAS-1, coul, ecran);	//draw the rectangle
		}
	}else{
		int x=((a%90)*PAS), y=((a/90)*PAS);
		rect(x+1, y+1, PAS-1, PAS-1, couleurs[C_ROUGE], ecran);	//draw the cell in red (this is the position of the "instruction pointer"
	}
}

Instruction& Memoire::operator[](unsigned int i){ 
	return m_tmemoire[i%TAILLE_MEM]; //the memory has a circular organization. Thus 9378 <=> 1378
}
